<?php
/**
 * Created by PhpStorm.
 * User: TianFeng
 * Date: 2017/11/7
 * Time: 17:56
 */
namespace app\common\lib;
class Page {
    /*
        *   分页
        */
    public  function pages($count, $pageSize, $nowPage)
    {
        $num = intval(floor($count / $pageSize));
        $mod = $count % $pageSize;
        if ($mod > 0) {
            $pages = $num + 1;
        } else {
            $pages = $num;
        }

        $start = 1;
        $end = $pages;
        if ($nowPage - 4 > ($start - 1)) {
            $start = $nowPage - 4;
        }

        if ($nowPage + 6 < $end) {
            $end = $nowPage + 6;
        }

        if ($nowPage > 1) {
            $prev = $nowPage - 1;
        } else {
            $prev = 1;
        }

        if ($nowPage >= $pages) {
            $next = $pages;
        } else {
            $next = $nowPage + 1;
        }
        $fullUrl = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER["REQUEST_URI"];

        $pageHtml = '<div class="row">';
        $pageHtml .= '<div class="col-sm-5">';
        $pageHtml .= '<div class="dataTables_info" id="DataTables_Table_0_info" role="alert" aria-live="polite" aria-relevant="all">本页显示' . $pageSize . ' 条，共 ' . $count . ' 条</div>';
        $pageHtml .= '</div>';
        $pageHtml .= '<div class="col-sm-5">';
        $pageHtml .= '<div class="dataTables_paginate paging_simple_numbers" id="DataTables_Table_0_paginate">';
        $pageHtml .= '<ul class="pagination">';
        $pageHtml .= '<li class="paginate_button previous" aria-controls="DataTables_Table_0" tabindex="0" id="DataTables_Table_0_previous">';
        $pageHtml .= '<a href="' . $this->urlSet($fullUrl, ['page' => 1]) . '">首页</a>';
        $pageHtml .= '</li>';
        $pageHtml .= '<li class="paginate_button previous" aria-controls="DataTables_Table_0" tabindex="0" id="DataTables_Table_0_previous">';
        $pageHtml .= '<a href="' . $this->urlSet($fullUrl, ['page' => $prev]) . '">上一页</a>';
        $pageHtml .= '</li>';
        for ($i = $start; $i <= $end; $i++) {
            if ($nowPage == $i) {
                $pageHtml .= '<li class="paginate_button active" aria-controls="DataTables_Table_0" tabindex="0"><a href="' . $this->urlSet($fullUrl, ['page' => $i]) . '">' . $i . '</a></li>';
            } else {
                $pageHtml .= '<li class="paginate_button" aria-controls="DataTables_Table_0" tabindex="0"><a href="' . $this->urlSet($fullUrl, ['page' => $i]) . '">' . $i . '</a></li>';
            }
        }
        $pageHtml .= '<li class="paginate_button next" aria-controls="DataTables_Table_0" tabindex="0" id="DataTables_Table_0_next">';
        $pageHtml .= '<a href="' . $this->urlSet($fullUrl, ['page' => $next]) . '">下一页</a>';
        $pageHtml .= '</li>';
        $pageHtml .= '<li class="paginate_button next" aria-controls="DataTables_Table_0" tabindex="0" id="DataTables_Table_0_next">';
        $pageHtml .= '<a href="' . $this->urlSet($fullUrl, ['page' => $pages]) . '">尾页</a>';
        $pageHtml .= '</li>';
        $pageHtml .= '</ul>';
        $pageHtml .= '</div>';
        $pageHtml .= '</div>';
        $pageHtml .= '</div>';
        return $pageHtml;
    }
    
    /**
     * ajax 分页
     * @param type $count
     * @param type $pageSize
     * @param type $nowPage
     * @return string
     */
    public function ajaxPages($count, $pageSize, $nowPage)
    {
        $num = intval(floor($count / $pageSize));
        $mod = $count % $pageSize;
        if ($mod > 0) {
            $pages = $num + 1;
        } else {
            $pages = $num;
        }

        $start = 1;
        $end = $pages;
        if ($nowPage - 4 > ($start - 1)) {
            $start = $nowPage - 4;
        }

        if ($nowPage + 6 < $end) {
            $end = $nowPage + 6;
        }

        if ($nowPage > 1) {
            $prev = $nowPage - 1;
        } else {
            $prev = 1;
        }

        if ($nowPage >= $pages) {
            $next = $pages;
        } else {
            $next = $nowPage + 1;
        }
        $fullUrl = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER["REQUEST_URI"];

        $pageHtml = '<div class="row">';
        $pageHtml .= '<div class="col-sm-5">';
        $pageHtml .= '<div class="dataTables_info" id="DataTables_Table_0_info" role="alert" aria-live="polite" aria-relevant="all">本页显示' . $pageSize . ' 条，共 ' . $count . ' 条</div>';
        $pageHtml .= '</div>';
        $pageHtml .= '</div>';
        $pageHtml .= '<div class="row center-block">';
        $pageHtml .= '<div class="col-sm-12">';
        $pageHtml .= '<div class="text-center dataTables_paginate paging_simple_numbers" id="DataTables_Table_0_paginate">';
        $pageHtml .= '<ul class="pagination">';
        $pageHtml .= '<li class="paginate_button next home" aria-controls="DataTables_Table_0" tabindex="0" id="DataTables_Table_0_previous">';
        $pageHtml .= '<a href="javascript:void(0);" page="1">首页</a>';
        $pageHtml .= '</li>';
        /* $pageHtml .= '<li class="paginate_button previous" aria-controls="DataTables_Table_0" tabindex="0" id="DataTables_Table_0_previous">';
          $pageHtml .= '<a href="javascript:void(0);" page='.$prev.' >上一页</a>';
          $pageHtml .= '</li>'; */
        for ($i = $start; $i <= $end; $i++) {
            if ($nowPage == $i) {
                $pageHtml .= '<li class="paginate_button active" aria-controls="DataTables_Table_0" tabindex="0">
                                <a href="javascript:void(0);" page="' . $i . '">' . $i . '</a>
                               </li>';
            } else {
                $pageHtml .= '<li class="paginate_button" aria-controls="DataTables_Table_0" tabindex="0">
                                <a href="javascript:void(0);" page="' . $i . '">' . $i . '</a>
                                </li>';
            }
        }
        /* $pageHtml .= '<li class="paginate_button next" aria-controls="DataTables_Table_0" tabindex="0" id="DataTables_Table_0_next">';
          $pageHtml .= '<a href="javascript:void(0);" page="'.$next.'">下一页</a>';
          $pageHtml .= '</li>'; */
        $pageHtml .= '<li class="paginate_button next last" aria-controls="DataTables_Table_0" tabindex="0" id="DataTables_Table_0_next">';
        $pageHtml .= '<a href="javascript:void(0);" page="' . $pages . '">尾页</a>';
        $pageHtml .= '</li>';
        $pageHtml .= '</ul>';
        $pageHtml .= '</div>';
        $pageHtml .= '</div>';
        $pageHtml .= '</div>';
        $pageHtml .= '</div>';
        return $pageHtml;
    }

//url参数提取以及替换
    private function urlSet($url, $params)
    {
        $urlarr = parse_url($url);
        if (isset($urlarr['query'])) {
            parse_str($urlarr['query'], $parr);
            foreach ($params as $key => $value) {
                $parr[$key] = $value;
            }
            $urlarr['query'] = http_build_query($parr);
        } else {
            $urlarr['query'] = http_build_query($params);
        }

        if (isset($urlarr['port'])) {
            return $urlarr['scheme'] . "://" . $urlarr['host'] . ":" . $urlarr['port'] . $urlarr['path'] . "?" . $urlarr['query'];
        } else {
            return $urlarr['scheme'] . "://" . $urlarr['host'] . $urlarr['path'] . "?" . $urlarr['query'];
        }
    }

}